﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceWarrantyRecordAuditInfoService
    {
        /// <summary>
        /// 获取已经审核的项
        /// </summary>
        /// <param name="insuranceWarrantyRecordId"></param>
        /// <param name="auditWith"></param>
        /// <param name="auditProjectConfigurationID"></param>
        /// <returns></returns>
        public InsuranceWarrantyRecordAuditInfo Load(long insuranceWarrantyRecordId, byte auditWith,
            int auditProjectConfigurationID)
        {
            return
                LoadEntities(
                    o =>
                        o.InsuranceWarrantyRecordID == insuranceWarrantyRecordId && o.AuditWith == auditWith &&
                        o.AuditProjectConfigurationID == auditProjectConfigurationID).FirstOrDefault();

        }
        /// <summary>
        /// 保存审核信息
        /// </summary>
        /// <param name="insuranceWarrantyRecordId"></param>
        /// <param name="insuranceWarrantyRecordAuditInfo"></param>
        /// <param name="opreateUser"></param>
        /// <returns></returns>
        public bool Save(long insuranceWarrantyRecordId,
            IList<InsuranceWarrantyRecordAuditInfo> insuranceWarrantyRecordAuditInfo, string opreateUser)
        {
            var result = true;
            var fieldOptionService = ServiceFactory.AuditProjectFieldOptionServiceInstance;
            if (insuranceWarrantyRecordAuditInfo.Any())
            {
                for (int i = 0; i < insuranceWarrantyRecordAuditInfo.Count; i++)
                {
                    if (!result)
                        return false;
                    var insuranceWarrantyRecordAuditInfoModel = insuranceWarrantyRecordAuditInfo[i];
                    if (string.IsNullOrEmpty(insuranceWarrantyRecordAuditInfoModel.AuditResult))
                    {
                        //请选择审核结果
                        continue;
                    }
                    var auditInfoEntity = Load(insuranceWarrantyRecordId,
                        insuranceWarrantyRecordAuditInfoModel.AuditWith,
                        insuranceWarrantyRecordAuditInfoModel.AuditProjectConfigurationID);
                    if (!string.IsNullOrEmpty(insuranceWarrantyRecordAuditInfoModel.AuditResult))
                    {
                        var optionIDs = new List<int>();
                        try
                        {
                            optionIDs = insuranceWarrantyRecordAuditInfoModel
                                .AuditResult.Split(',').ToList().ConvertAll(int.Parse);
                        }
                        catch (Exception)
                        {
                        }
                        insuranceWarrantyRecordAuditInfoModel.AuditResultText = fieldOptionService.GetOptionsFieldNames(optionIDs);
                    }
                    if (auditInfoEntity != null)
                    {
                        auditInfoEntity.AuditRemark = insuranceWarrantyRecordAuditInfoModel.AuditRemark;
                        auditInfoEntity.AuditResult = insuranceWarrantyRecordAuditInfoModel.AuditResult;
                        auditInfoEntity.AuditResultText = insuranceWarrantyRecordAuditInfoModel.AuditResultText;
                        auditInfoEntity.AuditWith = insuranceWarrantyRecordAuditInfoModel.AuditWith;
                        result = UpdateEntities(auditInfoEntity);
                    }
                    else
                    {
                        insuranceWarrantyRecordAuditInfoModel.InsuranceWarrantyRecordID = insuranceWarrantyRecordId;
                        insuranceWarrantyRecordAuditInfoModel.CreatedBy = opreateUser;
                        insuranceWarrantyRecordAuditInfoModel.CreatedTime = DateTime.Now;
                        result = AddEntities(insuranceWarrantyRecordAuditInfoModel).ID > 0;
                    }
                }
            }
            return result;
        }

    }
}
